<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="./css/base.css">
    <style>
        .crumbs {
            font-size: 14px;
            color: #ccc;
        }
    </style>
</head>

<body>
    <div class="container">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#" style="display:flex;">
                        小罗的博客
                    </a>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="/">首页</a></li>
                        <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                    aria-haspopup="true" aria-expanded="false">分类 <span class="caret"></span></a>
                                <ul class="dropdown-menu" id="cates">
                                   
                                </ul>
                            </li>
                        <li><a href="/tags.html">标签</a></li>
                        <li><a href="/about.html">关于我</a></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </div><!-- /.container-fluid -->
        </nav>
        <div style="text-align:center;" id="info">
        
        </div>
        <div class="content">
            <div style="text-align: center;">
                <img  id="img" style="width: 360px;" src="http://placehold.it/200x140/ffde00/aaabbb?text=xiaoluo" alt="">
            </div>
            <p id="content"></p>
        </div>
        <hr>
        <div class="footer" style="text-align:center;font-size: 14px; color: #999;margin:10px 0;">
            首页 | 主站 | GitHub | Powered by hexo | Theme china
        </div>
    </div>
</body>
<script src="./js/jquery.js"></script>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.6.8/layui.js"></script>
<script src="https://momentjs.bootcss.com/downloads/moment.min.js"></script>
<script>
    //参数的校验
    let url = location.href;
    let reg = /\?id=(.+)\&?/;
    let [,id] = Array.from(url.match(reg));
    if (isNaN(parseInt(id))) {
        layer.msg('请求错误')
        location.href = '/'
    }
    // 渲染文章详情
    function loadArticle(id) {
        $.get('http://127.0.0.1:8080/api/detail', {id}, res => {
            console.log(res);
            //如果是空对象,回到首页
            if (JSON.stringify(res) === '{}') {
                location.href = '/'
                return;
            }
            let { title, cate_name, cat_id, content, img, author,add_time } = res;
            // 拼接数据
           add_time = (moment(add_time).format('YYYY-MM-DD'))
            let htmlStr = `
            <h4 class="crumbs" style="text-align:left;">当前位置：首页 /${cate_name} / ${title}</h4>
            <h3>${title}</h3>
            <div style="font-size: 14px; color: #999;"> 2019-04-26 /
                分类：${cate_name} / 点赞：888 /作者：${author}
            </div>`
            $('#info').html(htmlStr)
            $('#content').text(content)
            if (img) {
                $('#img').attr('src', 'http://127.0.0.1:8080/' + img)
            } 
        }, 'json')
    }
     //渲染头部分类数据
     function renderCate(cates) {
        let { result } = cates;
        let lis = '';
        result.forEach((cat) => {
            let { id, cate_name } = cat;
            // 拼接字符串li
            lis += `<li><a href="/cate.html?id=${id}">${cate_name}</a></li>`
        });
        return lis;
    }

    // 头部分类
    function loadCate() {
        return $.ajax({
            url: "http://127.0.0.1:8080/api/cate"
        })
    }
   
   

    loadArticle(id)

    let cates = JSON.parse(localStorage.getItem('cates') || '[]');
    if (cates.length) {
        let lis = renderCate(cates)
        $('#cates').html(lis)
    } else {
        loadCate().then(res => {
            let lis = renderCate(res)
            $('#cates').html(lis)
        })
    }
</script>

</html>